Tutorial / Make a button
This tutorial will teach you how to make a button. This button will simply quit your game when you click on it. First, you need a button model. I will assume you know how to do it, this is not the point of this course. I will use this one, coming from "Doomsday Carrot Rampage". Once you have your model, create a scene, put a camera (called "camera") object in it and an object with a model renderer with your button as model. Step-by-step scripting Now, we need our button to be "clickable". Create a new script, you should get this: function Behavior:Awake() end function Behavior:Update() end Behavior:Awake() '''is called only once when the game is launched. In this function we will initialize some variable used to acces our objects easily later. Firstly, we need to get the model renderer belonging to our button object, we will create a variable called "modelRndr". Assuming our script is attached to it, we acces the object with "self.gameObject" and we need his component "ModelRenderer" : self.modelRndr = self.gameObject:GetComponent("ModelRenderer" ) Then, in the same way, we need the "Camera" component from our "camera" object. self.camera = CraftStudio.FindGameObject( "camera" ):GetComponent( "Camera" ) So we have our Behavion:Awake() function : function Behavior:Awake() self.modelRndr = self.gameObject:GetComponent("ModelRenderer" ) self.camera = CraftStudio.FindGameObject( "camera" ):GetComponent( "Camera" ) end '''Behavior:Update()? is called one time per frame, so 60 time per second. This function wil continusly check if the user click on the button. In the administration tab of your project, in the control tab, create a control called "Fire" link to the mouse right clic. So, we have to check if the button "Fire" have been pressed : if CraftStudio.Input.WasButtonJustPressed("Fire")then end If '''is a conditinal statement, every thing between "then" and "end" will be run only if our condition is true. Then, we have to check if the mouse cursor is on the button. To do it, we will cast a ray from our camera to our cursor. To create this ray, we need the actual position of our cursor. We can get it with : local mousePos = CraftStudio.Input.GetMousePosition() Our variable "mousePos" will contain our cursor position. Now, we can create our ray : local ray = self.camera:CreateRay( mousePos ) Our variable "ray", contain a ray starting at the camera position and ? pass? through? the cursor. Lastly, we check if the ray pass? through our model. We make an other condition nested in the first one : if ray:IntersectsModelRenderer( self.modelRndr ) ~= nil then end '''ray:IntersectsModelRenderer( self.modelRndr )? is our conditionest. This function will return the distance between the starting point of our ray and the intersection point with "self.modelRndr". If there is no intersection, the fubnction return "nil". The symbol "~=" means "different of". 'Nil '''is the value for an empty varaible. So, we can translate our "if" statement by: "If the intersection distance is not nul (the intersection exist so the ray touch the buton)" The last used function, is used to quit our game : CraftStudio.Exit() I think it seem obvious. Now, return in your scene, and add a script component to the button object then select your script. The whole script Here is our final script : function Behavior:Awake() self.camera = CraftStudio.FindGameObject( "camera" ):GetComponent( "Camera" ) self.modelRndr = self.gameObject:GetComponent( "ModelRenderer" ) end function Behavior:Update() if CraftStudio.Input.WasButtonJustPressed("clicgauche") then local mousePos = CraftStudio.Input.GetMousePosition() local ray = self.camera:CreateRay( mousePos ) local distance = ray:IntersectsModelRenderer( self.modelRndr ) if ray:IntersectsModelRenderer( self.modelRndr ) ~= nil then CraftStudio.Exit() end end end I hope this tutorial is clear enough and teaches you something! ''Wardow (translated from french by Jaunmakenro)